/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.farent.solutions.plugins.stripes.hyperstripes.core.tags;

import com.farent.solutions.plugins.stripes.hyperstripes.core.ajax.AjaxResponseInsert;
import com.farent.solutions.plugins.stripes.hyperstripes.core.Attribute;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentModel;
import com.farent.solutions.plugins.stripes.hyperstripes.core.ComponentTagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.core.FormMode;
import com.farent.solutions.plugins.stripes.hyperstripes.core.TagHandler;
import com.farent.solutions.plugins.stripes.hyperstripes.utils.Utils;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.mvel2.templates.CompiledTemplate;
import org.mvel2.templates.TemplateCompiler;
import org.mvel2.templates.TemplateRuntime;

/**
 *
 * @author farouka
 */
@TagHandler("input")
public class InputTagHandler implements ComponentTagHandler
{
    @Override
    public String createContent( ComponentModel model, Object bindings)
    {
        return processContent(model, bindings);
    }

    String processContent( ComponentModel model, Object bindings)
    {
        String out = Utils.EMPTY_STRING;
        try {
            InputComponentModel bcm = (InputComponentModel) model;
            Attribute value = bcm.getAttribute("value");
            if( value != null && !Utils.nullOREmpty( value.getValue() ) )
            {
                bcm.setText( value.getValue() );
            }
            if( Utils.nullOREmpty( bcm.getText() ) )
            {
                bcm.setText(Utils.EMPTY_STRING);
            }
            Map var = new HashMap();
            var.put("model", bcm);
            InputStream template3 = InputTagHandler.class.getResourceAsStream("input_edit.tag");
            FormMode mode = bcm.getMode();
            if (mode == FormMode.VIEW) {
                template3 = InputTagHandler.class.getResourceAsStream("input_view.tag");
            }
            out = processTemplate(template3, bindings, var);
        } catch (UnsupportedEncodingException ex) {
            Logger.getLogger(InputTagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return out;
    }

    public String processTemplate(InputStream template, Object bindings, Map var) throws UnsupportedEncodingException {
        CompiledTemplate compiledTemplate = TemplateCompiler.compileTemplate(template);

        String out = (String) TemplateRuntime.execute(compiledTemplate, bindings, var);
        while( out.contains("@{")){
            InputStream is = new ByteArrayInputStream(out.getBytes("UTF-8"));
            out = processTemplate(is, bindings, var);
        }
        return out;
    }

    @Override
    public String createScripts(ComponentModel model) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public AjaxResponseInsert createAjaxInsert(ComponentModel model, Object bindings) {
        AjaxResponseInsert responseInsert = new AjaxResponseInsert();
        String output =  processContent(model, bindings);
        responseInsert.setElementValue(output);
        return responseInsert;
    }

    @Override
    public void createContent(ComponentModel model, Object bindings, OutputStream outputStream) {
        try {
            InputComponentModel bcm = (InputComponentModel) model;
            Attribute value = bcm.getAttribute("value");
            if( value != null && !Utils.nullOREmpty( value.getValue() ) )
            {
                bcm.setText( value.getValue() );
            }
            if( Utils.nullOREmpty( bcm.getText() ) )
            {
                bcm.setText(Utils.EMPTY_STRING);
            }
            Map var = new HashMap();
            var.put("model", bcm);
            InputStream template3 = InputTagHandler.class.getResourceAsStream("input_edit.tag");
            FormMode mode = bcm.getMode();
            if (mode == FormMode.VIEW) {
                template3 = InputTagHandler.class.getResourceAsStream("input_view.tag");
            }
            String src = processTemplate(template3, bindings, var);
            InputStream srcStream = new ByteArrayInputStream(src.getBytes());
            byte[] buffer = new byte[ 512 ];
            try
            {
                int length = 0;
                while ( ( length = srcStream.read( buffer ) ) != -1 )
                {
                    outputStream.write( buffer, 0, length );
                }
            }
            finally
            {
                try
                {
                    srcStream.close();
                }
                catch ( Exception e )
                {
                    e.printStackTrace();
                }
            }
        } catch (Exception ex) {
            Logger.getLogger(InputTagHandler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}
